﻿// Function Variable
// ================================================
var message = {};               //  ajax model
var butonName = '#convert';
var formName = 'form';

var contentFrom = '#contentFrom';
var contentTo = '#contentTo';

var ajaxData;                   //  post string
var ajaxAction;                 //  Api Url
var ajaxData;
var ajaxContentType = false;
var ajaxProcessData = false;
var ajaxType = 'post';

var performCounter = 0;

// Form submit api posting progress
// ================================================
$(function ()
{
    popoverInit();

    submitEventInit();
});

// Form Submit Event Initalize
// ================================================
function submitEventInit()
{
    $(formName).submit(function (event)
    {
        event.preventDefault();

        getAjaxData();
        $(butonName).popover('show');
        
        ajaxPost();
    });
}


// Ajax Post
// ================================================
function ajaxPost()
{
    

    $.ajax(
        {
            beforeSend: performStart,
            type: ajaxType,
            url: ajaxAction,
            data: ajaxData,
            contentType: ajaxContentType,
            processData: ajaxProcessData,
            success: succesEvent,
            error: errorEvent
        });
}


// Popover Initalize
// ================================================
function popoverInit()
{
    $(butonName).popover(
        {
            placement: 'left',
            delay: { show: 100, hide: 1 },            
            trigger: 'manual',
            hide: function () {
            }
        });
}

// Media Type
// ================================================
function getAjaxData()
{
    ajaxAction = $(formName).attr("action");

    if ($(formName).attr("enctype") == "multipart/form-data")
    {
        ajaxData = new FormData($(formName).get(0));
    }
    else
    {
    }
}

function sourceFile(content)
{
    $(contentFrom).empty();
    $(contentFrom).append(content);
}

function exportContent(content) {
    $(contentTo).empty();
    $(contentTo).append(content);
}

// Ajax Succes Event
// ================================================
function succesEvent(_message) {
    performCompleted();

    MessageWrite(_message);

    sourceFile(_message.file.content);

    exportContent(_message.export);
}
// Ajax Error Event
// ================================================
function errorEvent(jqXHR, textStatus, errorThrown)
{
    performCompleted();

    var _message =
        {
            statusText: "File Error",
            responseText: "Err",
            responseObject: errorThrown,
            readyState: 0,
            Status: 0
        };
    
    MessageWrite(_message);
}

// Popover message writer
// ================================================
function MessageWrite(_message)
{
    message = _message;

    $('.popover .popover-content').append(
        message.statusText + ' <br /> ' +
        ''
        );
}

// Performans Counter
// ================================================
function performStart() {
    $('.popover .popover-content').empty();

    performCounter = Date.now();
}

function performCompleted() {
    performCounter = Date.now() - performCounter;
    $('.popover .popover-content').append("perform : " + performCounter + 'ms <br /> ');
}